﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AnalysisSystem
{
    class Correlation
    {
        public static DataTable CorrelationTable = new DataTable();
        public static void GetCorrelationTable()
        {
            CreateInitialTable();
            for (int i = 0; i < CorrelationTable.Rows.Count - 1; i++)
            {
                for (int j = 1; j < CorrelationTable.Columns.Count; j++)
                {
                    List<double> x = new List<double>();
                    List<double> y = new List<double>();
                    string xName = CorrelationTable.Rows[i][0].ToString();
                    string yName = CorrelationTable.Columns[j].ColumnName;
                    if (xName.Equals(yName)) CorrelationTable.Rows[i][j] = 1.00;
                    else
                    {
                        for (int k = 0; k < SingleWellSummary.singleWellSumTable.Rows.Count; k++)
                        {
                            x.Add(Convert.ToDouble(SingleWellSummary.singleWellSumTable.Rows[k][xName]));
                            y.Add(Convert.ToDouble(SingleWellSummary.singleWellSumTable.Rows[k][yName]));
                        }
                        CorrelationTable.Rows[i][j] = Math.Round(MathCorrel(x, y), 3);
                    }
                }
            }
        }
        //建表
        private static void CreateInitialTable()
        {
            CorrelationTable.TableName = "7-相关性";            
            if (SingleWellSummary.singleWellSumTable.Rows.Count == 0)
                SingleWellSummary.SingleWellSum();
            List<string> colName = new List<string>();
            for(int i=10; i<SingleWellSummary.singleWellSumTable.Columns.Count; i++)
            {
                colName.Add(SingleWellSummary.singleWellSumTable.Columns[i].ColumnName);
            }
            for(int i=0; i<=colName.Count; i++)
            {
                CorrelationTable.Rows.Add();
                CorrelationTable.Columns.Add();
                if (i == 0) CorrelationTable.Columns[i].ColumnName = "名称";
                else CorrelationTable.Columns[i].ColumnName = colName[i - 1];
            }
            for(int i=0; i<colName.Count; i++)
            {                
                CorrelationTable.Rows[i][0] = colName[i];
            }            
        }
        //相关性计算
        private static double MathCorrel(in List<double> x, in List<double> y)
        {
            if (x.Count != y.Count) return -1;
            double result = 0, numerator=0, xDenominator = 0, yDenominator = 0, denominator = 0;
            double xAve = MathAverage(x);
            double yAve = MathAverage(y);
            if (xAve == -1 || yAve == -1) return -1;
            for(int i=0; i<x.Count; i++)
            {
                numerator += (x[i] - xAve) * (y[i] - yAve);
                xDenominator += (x[i] - xAve) * (x[i] - xAve);
                yDenominator += (y[i] - yAve) * (y[i] - yAve);
            }
            denominator = Math.Sqrt(xDenominator * yDenominator);
            if (denominator.Equals(0)) return -1;
            result = numerator / denominator;
            return result;
        }
        //平均数计算
        private static double MathAverage(in List<double> value)
        {
            if (value.Count == 0) return -1;
            double average = 0;
            for(int i=0; i<value.Count; i++)
            {
                average += value[i];
            }
            average = average / value.Count;
            return average;
        }
    }
}
